package models.issue;

import models.UUIDModel;
import models.admin.AdminUser;
import models.campus.Campus;
import models.campus.Room;
import models.constants.DeletedStatus;
import models.enums.*;
import models.system.Company;
import models.user.User;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by hanbin
 * on 2017/1/11.
 * Time : 上午1:11
 */
@Entity
@Table(name = "is_issues")
public class Issue extends UUIDModel {

    /**
     * 名称
     */
    @Column(name = "issue_name", length = 500)
    public String issueName;

    /**
     * 类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "issue_type", length = 10)
    public IssueType issueType;

    /**
     * 分支类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "issue_sub_type", length = 50)
    public IssueSubType issueSubType;

    /**
     * 报修人员
     */
    @JoinColumn(name = "user_id")
    @ManyToOne
    public User user;

    /**
     * 预估维修金额
     */
    @Column(name = "estimate_amount")
    public BigDecimal estimateAmount;

    /**
     * 报修的房间
     * 解决 一个用户多个房间的问题
     */
    @JoinColumn(name = "campus_room_id")
    @ManyToOne
    public Room room;

    /**
     * 公共报修小区
     */
    @Column(name = "campus_id")
    public String campusId;
    /**
     * 联系电话
     */
    @Column(name = "phone", length = 15)
    public String phone;

    /**
     * 上门日期
     */
    @Transient
    public String visitTimeStr;

    /**
     * 上门时间开始
     */
    @Column(name = "visit_time_begin")
    public Date visitTimeBegin;

    /**
     * 上门时间开始
     */
    @Transient
    public String visitTimeBeginStr;

    /**
     * 上门时间结束
     */
    @Column(name = "visit_time_end")
    public Date visitTimeEnd;

    /**
     * 上门截止时间
     */
    @Transient
    public String visitTimeEndStr;

    /**
     * 受理时间
     */
    @Column(name = "accept_time")
    public Date acceptTime;

    /**
     * 受理日期
     */
    @Transient
    public String acceptTimeStr;

    /**
     * 受理日期
     */
    @Transient
    public String createdAtStr;

    /**
     * 当前服务人员
     */
    @JoinColumn(name = "service_user_id")
    @ManyToOne
    public AdminUser serviceUser;

    /**
     * 描述说明
     */
    @Column(name = "remark", length = 500)
    public String remark;

    /**
     * 状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "issue_status", length = 15)
    public IssueStatus issueStatus = IssueStatus.CREATE;

    /**
     * 账单状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "issue_voucher_status", length = 15)
    public IssueVoucherStatus issueVoucherStatus = IssueVoucherStatus.NO_VOUCHER;


    public Issue() {
        super();
    }

    public Issue(String issueName, String phone, User user, Room room, String campusId,IssueSubType issueSubType, IssueType issueType, String remark, Date visitTimeBegin, Date visitTimeEnd) {
        this.issueName = issueName;
        this.phone = phone;
        this.user = user;
        this.room = room;
        this.campusId = campusId;
        this.issueType = issueType;
        this.issueSubType = issueSubType;
        this.issueStatus = IssueStatus.CREATE;
        this.remark = remark;
        this.deleted = DeletedStatus.UN_DELETED;
        this.createdAt = new Date();
        this.visitTimeBegin = visitTimeBegin;
        this.visitTimeEnd = visitTimeEnd;
        this.belongCompany = user.belongCompany != null ? user.belongCompany : null ;
    }

    /**
     * 查找未删除
     *
     * @param id
     * @return
     */
    public static Issue findByIdAndUnDeleted(String id) {
        return Issue.find("id = ?1 and deleted = ?2", id, DeletedStatus.UN_DELETED).first();
    }


    /**
     * 根据微信用户和当前状态查找工单
     *
     * @param user
     * @param issueStatus
     * @return
     */
    public static List<Issue> findByWeixinUserAndStatus(User user, IssueStatus issueStatus) {
        return Issue.find("serviceUser.weiXinUser = ? and deleted = ? and issueStatus = ?", user, DeletedStatus.UN_DELETED, issueStatus).fetch();
    }

    public static List<Issue> findByWeixinUserAndType(User user, IssueType issueType) {
        return Issue.find("user = ? and deleted = ? and issueType = ? ",user,DeletedStatus.UN_DELETED,issueType).fetch();
    }

    /**
     * 不希望使用该方法
     * @param adminUser
     * @return
     */
    public static List<Issue> findByServiceUser(AdminUser adminUser) {
        return Issue.find("serviceUser = ? and deleted = ? order by createdAt desc",adminUser,DeletedStatus.UN_DELETED).fetch();
    }

    /**
     * 不希望使用该方法
     * @param adminUser
     * @param issueStatus
     * @return
     */
    public static List<Issue> findByServiceUserAndStatus(AdminUser adminUser,IssueStatus issueStatus) {

        return Issue.find("serviceUser = ? and deleted = ? and issueStatus = ? order by createdAt desc",adminUser,DeletedStatus.UN_DELETED,issueStatus).fetch();
    }

    public static List<Issue> findByServerUserAndCompany(AdminUser adminUser, Company company) {
        return Issue.find("serviceUser = ? and deleted = ? and belongCompany = ?  order by createdAt desc",adminUser,DeletedStatus.UN_DELETED,company).fetch();
    }

    /**
     * 根据维修人员以及状态获取维修单
     * @param adminUser
     * @param issueStatus
     * @return
     */
    public static List<Issue> findByDetailOprateAndStatus(AdminUser adminUser,IssueStatus issueStatus) {
        List<IssueDetail> issueDetailList = IssueDetail.find("operator = ? and issueDetailStatus = ? and deleted = ? and issue.deleted = ? and issue.issueStatus = ? order by createdAt desc",adminUser.id+"", IssueDetailStatus.NORMAL,DeletedStatus.UN_DELETED,DeletedStatus.UN_DELETED,issueStatus).fetch();
        List<Issue> issueList = new ArrayList<Issue>();
        Map<String,Object> map = new HashMap<String,Object>();
        for (IssueDetail issueDetail : issueDetailList) {
            if(map.get(issueDetail.issue.id) == null) {
                map.put(issueDetail.issue.id,issueDetail);
                issueList.add(issueDetail.issue);
            }
        }
        return issueList;
    }

    /**
     * 根据维修人员以及两种状态获取
     * @param adminUser
     * @param issueStatus
     * @return
     */
    public static List<Issue> findByDetailOprateAndDoubleStatus(AdminUser adminUser,IssueStatus issueStatus,IssueStatus issueStatus1) {
        List<IssueDetail> issueDetailList = IssueDetail.find("operator = ? and issueDetailStatus = ? and deleted = ? and issue.deleted = ? and (issue.issueStatus = ? or issue.issueStatus = ?) order by createdAt desc",adminUser.id+"",IssueDetailStatus.NORMAL,DeletedStatus.UN_DELETED,DeletedStatus.UN_DELETED,issueStatus,issueStatus1).fetch();
        List<Issue> issueList = new ArrayList<Issue>();
        Map<String,Object> map = new HashMap<String,Object>();
        for (IssueDetail issueDetail : issueDetailList) {
            if(map.get(issueDetail.issue.id) == null) {
                map.put(issueDetail.issue.id,issueDetail);
                issueList.add(issueDetail.issue);
            }
        }
        return issueList;
    }

    /**
     * 根据维修人员获取所有维修单
     * @param adminUser
     * @return
     */
    public static List<Issue> findByDetailOprate(AdminUser adminUser) {
        List<IssueDetail> issueDetailList = IssueDetail.find("operator = ? and issueDetailStatus = ? and deleted = ? and issue.deleted = ? order by createdAt desc",adminUser.id+"",IssueDetailStatus.NORMAL,DeletedStatus.UN_DELETED,DeletedStatus.UN_DELETED).fetch();
        List<Issue> issueList = new ArrayList<Issue>();
        Map<String,Object> map = new HashMap<String,Object>();
        for (IssueDetail issueDetail : issueDetailList) {
            if(map.get(issueDetail.issue.id) == null) {
                map.put(issueDetail.issue.id,issueDetail);
                issueList.add(issueDetail.issue);
            }
        }
        return issueList;
    }


}
